import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';

import '../../../../data/controllers/gyms_controller.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../../gym_info_page/gym_info_screen.dart';
import '../../../gyms/gyms_page/models/gym.dart';
import '../models/public_gym.dart';

//35.5407103, 35.7952667
class PublicGymItemView extends StatefulWidget {
  final PublicGym gym;
  const PublicGymItemView({super.key, required this.gym});

  @override
  State<PublicGymItemView> createState() => _PublicGymItemViewState();
}

class _PublicGymItemViewState extends State<PublicGymItemView> {
  @override
  Widget build(BuildContext context) {

    return InkWell(
      onTap: (){
         Navigator.push(context, MaterialPageRoute(builder: (context) => GymInfoScreen.builder(context,Gym.fromPublicGym(widget.gym),true)));
      },
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Card(
          color: Theme.of(context).colorScheme.secondary,
          margin:const EdgeInsets.symmetric(vertical: 4,horizontal: 8),
          child: Container(
            height: 220,
            decoration: BoxDecoration(
              color: Theme.of(context).colorScheme.secondary,
              image: DecorationImage(
                  fit:(widget.gym.image==null)? null:BoxFit.cover,
                  isAntiAlias: true,
                  onError: (obj , st){
                    setState(() {
                      widget.gym.image= null;
                    });
                  },
                  image:(widget.gym.image!=null)?CachedNetworkImageProvider(widget.gym.image!):Image.asset(ImageConstant.imgLogo).image
              ),
            ),
            child: Container(
              padding:const EdgeInsets.all(4),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Colors.black,
                    Colors.black.withOpacity(0),
                    Colors.black,
                  ],
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(widget.gym.name, style: theme.textTheme.titleMedium?.copyWith(color: Colors.redAccent),),
                                Row(
                                  children: [
                                    const Icon(Icons.group,color: Colors.orangeAccent,),
                                    const SizedBox(width: 2,),
                                    Text(widget.gym.recentAttendanceCount.toString(), style: theme.textTheme.titleSmall?.copyWith(color: Colors.orangeAccent),),
                                  ],
                                )
                              ],
                            ),
                            SizedBox(
                              child: Text(
                                widget.gym.address,
                                maxLines: 1,
                                style: theme.textTheme.titleMedium!.copyWith(fontSize: 14,color: Colors.white),),
                            ),
                          ],
                        ),
                      ),

                    ],
                  ),
                  Align(
                    alignment: Alignment.centerRight,
                    child: Text(
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      widget.gym.description,
                      style: theme.textTheme.titleSmall?.copyWith(color: Colors.white),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void showDeleteConfirmation(BuildContext context, PublicGym myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: myGym.name,
          titleUser: 'النادي',
          onConfirm: () {
            GymsController.deleteById(widget.gym.gymId??'-1');
            if (kDebugMode) {
              print('تم حذف النادي ${widget.gym.name}!');
            }
          },
        );
      },
    );
  }
}
